// pages/mystore/logo/index.js
import { getQiniuToken, cdnUrl, apiUrls, makeRequest } from '../../../utils/api';
import { uploadQiniu } from '../../../utils/upload';
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: '',
    id:0,
    logo_local: '',
    qiniu_token: '',
    isAgree: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      id: options.id
    })
    that.wxValidate = App.wxValidate(
      {
        logo: {
          required: true,
        }
      }
      , {
        logo: {
          required: '店铺Logo必须上传',
        }
      }
    );
    makeRequest({
      url: apiUrls.getShopDetail(options.id),
      method: 'GET',
      data: {
        include: 'category,manager,area'
      },
      success: function (res) {
        let logo_local='';
        if (res.data.data.logo === '') {
          logo_local = cdnUrl + 'hxy.png-c128'
        } else {
          logo_local = cdnUrl + res.data.data.logo+'-c128'
        }
        that.setData({
          logo_local: logo_local,
          logo: res.data.data.logo
        })
      }
    });

    makeRequest({
      url: apiUrls.getQiniuToken,
      method: 'GET',
      success: function (res) {
        that.setData({ qiniu_token: res.data.uptoken });
      }
    });
  },
  bindSubmit(e) {
    let that= this;
    //提交错误描述
    if (!this.wxValidate.checkForm(e)) {
      const error = this.wxValidate.errorList[0]
      // `${error.param} : ${error.msg} `
      wx.showToast({
        title: `${error.msg} `,
        image: '/images/error.png',
        duration: 500
      })
      return false
    }
    else {
      makeRequest({
        url: apiUrls.putLogoEdit(that.data.id),
        method: 'PUT',
        data: e.detail.value,
        success: function (res) {
          console.log(res);
          if (res.statusCode === 422) {
            wx.showToast({
              title: `${res.data.data[Object.keys(res.data.data)[0]][0]} `,
              image: '/images/error.png',
              duration: 500
            })
          } else if (res.statusCode === 200) {
            wx.showToast({
              title: '保存成功',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1000);
              }
            });
          }
        }
      });
    }
    //console.log(e.detail.value)
  },
  chooseLicenceImage() {
    const that = this
    wx.chooseImage({
      success: function (res) {
        const file = res.tempFilePaths[0];
        uploadQiniu(file, that.data.qiniu_token, function (res) {
          const key = JSON.parse(res.data).key;
          that.setData({
            logo_local: file,
            logo: key
          });
        });
      },
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.form.photos // 需要预览的图片http链接列表
    })
  },
  previewSingleImage(e) {
    wx.previewImage({
      current: 0, // 当前显示图片的http链接
      urls: [e.currentTarget.id] // 需要预览的图片http链接列表
    })
  },
})